﻿#region

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

using Pob.CustomEventArgs;
using Pob.GameObjects;

#endregion

namespace Pob.ScreenManagement.GameScreens
{
    public delegate void PauseScreenEvent(object sender, GameScreenEventArgs args);

    public class PausedScreen : ScreenBase
    {
        #region Fields

        private readonly MenuControl _control;

        private readonly MenuItemGroup _menuItemGroup;

        private bool _firstUpdate = true;

        #endregion

        #region Constructors and Destructors

        public PausedScreen(Game game, PauseScreenEvent eventHandler)
            : base(game)
        {
            Background = new Background(game, "PausedScreenBackground", new Vector2(0, 0), 1, Color.White * 0.5f);

            _menuItemGroup = new MenuItemGroup(game, GameContent.FontMenuItems, Color.White, OringeMode.MiddleCenter, DrawStringMode.WithSelected);
            _menuItemGroup.Add("Resume", OnResumeItem, true);
            _menuItemGroup.Add("Exit", OnExitItem);

            MyComponents.Add(Background);
            MyComponents.AddRange(_menuItemGroup.MenuItems);

            _control = new MenuControl(_menuItemGroup.MenuItems);

            GameResumed += eventHandler;
            GameExit += eventHandler;
        }

        #endregion

        #region Public Events

        public event PauseScreenEvent GameExit;

        public event PauseScreenEvent GameResumed;

        #endregion

        #region Public Methods and Operators

        public override void Initialize()
        {
            _menuItemGroup.CalculatePositions();

            AddRange(MyComponents);

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            if (!_firstUpdate)
            {
                _control.Update(gameTime);
            }
            else
            {
                // Input-Abfrage beginnt erst, wenn keine Keys mehr aus dem vorherigen Screen gedrückt sind
                _firstUpdate = Keyboard.GetState().GetPressedKeys().Length > 0;
            }
        }

        #endregion

        #region Methods

        private void OnExitItem(object obj, MenuItemPickedEventArgs args)
        {
            OnGameExit();
        }

        private void OnGameExit()
        {
            var args = new GameScreenEventArgs(null, GameState.Exit);

            if (GameExit != null)
            {
                GameExit(this, args);
            }
        }

        private void OnGameResumed()
        {
            var args = new GameScreenEventArgs(null, GameState.Resumed);

            if (GameResumed != null)
            {
                GameResumed(this, args);
            }
        }

        private void OnResumeItem(object obj, MenuItemPickedEventArgs args)
        {
            OnGameResumed();
        }

        #endregion
    }
}